package com.itheima.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.mapper.EmplMapper;
import com.itheima.pojo.Empl;
import com.itheima.pojo.PageBean;
import com.itheima.service.EmplService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;

@Service
public class EmplServiceImpl implements EmplService {
    @Autowired
    private EmplMapper emplMapper;

    //分页查询
    /*@Override
    public PageBean select(Integer page,Integer pagesize) {
        //查询数据总数
        Integer total=emplMapper.selectTotal();
        //将页码转换为开始查询的索引,进行分页查询
        Integer start=(page-1)*pagesize;
        List<Empl> rows=emplMapper.selectPage(start,pagesize);

        return new PageBean(total,rows);
    }*/

    //分页查询-插件实现
    @Override
    public PageBean select(Integer page, Integer pagesize) {
        //1.设置分页参数
        PageHelper.startPage(page,pagesize);
        //2.执行查询
        List<Empl>rows=emplMapper.selectByHelper();
        //3.转化格式
        Page<Empl> page1=(Page<Empl>) rows;

        return new PageBean((int) page1.getTotal(),page1.getResult());
    }
    //分页条件查询
    @Override
    public PageBean selectByChoice(Integer page, Integer pagesize, String name, Short gender, LocalDate begin, LocalDate end) {
        PageHelper.startPage(page,pagesize);
        List<Empl>rows=emplMapper.selectByChoice(name,gender,begin,end);
        Page<Empl> page1=(Page<Empl>) rows;

        return new PageBean((int) page1.getTotal(),page1.getResult());
    }
    //删除员工
    @Override
    public void delete(List<Integer> ids) {
        emplMapper.delete(ids);
    }
    //添加员工
    @Override
    public void insert(Empl empl) {
        empl.setCreateTime(LocalDateTime.now());
        empl.setUpdateTime(LocalDateTime.now());
        emplMapper.insert(empl);
    }
    //根据id查询员工
    @Override
    public Empl selectById(Integer id) {
        return emplMapper.selectById(id);
    }
    //更新员工信息
    @Override
    public void update(Empl empl) {
        empl.setUpdateTime(LocalDateTime.now());
        emplMapper.update(empl);
    }
    //登录模块
    @Override
    public Empl login(String username, String password) {
        return emplMapper.login(username,password);
    }
}
